<?php
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of ModPadres
 *
 * @author EaDeveloper
 */
class ModPadres implements EaSoftModule {

    public $title = "Padres de Familia";
    public $mPadres;
    public $relativePath = "modules/parents/";
    public $includePath = "parents/";
    public $db;
    public $grdAllEstudents;
    public $grdHijos;
    public $grdPadres;
    public $instanceName = "ModPadres";
    public $formName = "modpadresfamilia";
    public $widgets;

    public function __construct() {
        $this->widgets = new Widget();
        $this->widgets->rootPath = RELATIVE_PATH . "js_v2/";
        $this->mPadres = new Maintenance();
        $this->mPadres->table = "cllg_parent";
        $this->mPadres->title = 'Mantenimiento de Padres de Familia';
        $this->mPadres->validations = array("required,NOMBRE_A,1er Nombre es Requerido",
            "required,APELLIDO_A,1er Apellido es Requerido",
            "required,SEXO,Sexo es Requerido",
            "required,DOCTO_ID,Tipo documento es Requerido",
            "required,NUMERO_ID,Numero de identificacion es Requerido");

        $this->widgets->register("FEC_NAC", 2);

        $this->grdAllEstudents = new JGridGenerator();
        $query = "SELECT cllg_student.ID, cllg_student.FIRST_NAME, cllg_student.LAST_NAME,
               cllg_student_parent.PARENT
        FROM   cllg_student
               LEFT JOIN cllg_student_parent ON cllg_student.ID = cllg_student_parent.STUDENT
        WHERE  cllg_student_parent.PARENT IS NULL";
        $this->grdAllEstudents->query = $query;
        $this->grdAllEstudents->fields = array("ID", "FIRST_NAME", "LAST_NAME", "PARENT");
        $this->grdAllEstudents->pk = "ID";

        $this->grdHijos = new JGridGenerator();
        $query = "select s.ID,s.FIRST_NAME,s.SECOND_NAME,s.LAST_NAME,p.PARENT from cllg_student s
			inner join cllg_student_parent p on s.ID=p.STUDENT";
        $this->grdHijos->query = $query;
        $this->grdHijos->search = false;
        $this->grdHijos->fields = array("ID", "FIRST_NAME", "LAST_NAME", "APELLIDO_A", "PARENT");
        $this->grdHijos->pk = "ID";

        $this->grdPadres = new JGridGenerator();
        $this->grdPadres->table = "cllg_parent";
        $this->grdPadres->fields = array("ID", "NOMBRE_A", "APELLIDO_A");
        $this->grdPadres->colTypes = array("ID" => "int", "NOMBRE_A" => "text", "APELLIDO_A" => "text");
        $this->grdPadres->pk = "ID";
        $this->db = DbManager::getMe();
    }

    public function getCssFiles() {
        $this->mPadres->getCssFiles();
    }

    public function getJsFiles() {
        $this->mPadres->getJsFiles();
        ?>
        <script type="text/javascript">
            var AJAX_CORE_URL='<?php echo CORE_PATH ?>?module=<?php echo $this->instanceName; ?>&ajax=true';
        </script>
        <script type="text/javascript" src="<?php echo $this->relativePath; ?>sft.cllg.parents.js"  > </script>
        <script type="text/javascript" src="<?php echo $this->relativePath; ?>sft.cllg.listhijos.js"  > </script>
        <script type="text/javascript" >	jQuery.noConflict();</script>
        <script src="<?php echo RELATIVE_PATH; ?>js/prototype.js" type="text/javascript"></script>
        <script src="<?php echo RELATIVE_PATH; ?>js/jsvalidate.js" type="text/javascript"></script>
        <script type="text/javascript">
        <?php
        $this->mPadres->db = $this->db;
        echo $this->mPadres->loadDynValidations();
        echo $this->mPadres->scriptValidations();
        ?>
        </script>
        <?php
        $this->widgets->db = $this->db;
        $this->widgets->includes();
        $this->widgets->activateWidgets();
    }

    public function getWindowTitle() {
        return $this->title;
    }

    public function main() {
        if ($this->mPadres->builder == null) {
            $this->mPadres->init($this->db);
        }
        if (!isEmpty(getParam("operation"))) {
            switch (getParam("operation")) {
                case "parents":
                    $this->grdPadres->db = $this->db;
                    $this->grdPadres->main();
                    break;
                case "liststuds":
                    $this->grdAllEstudents->db = $this->db;
                    $this->grdAllEstudents->main();
                    break;
                case "listhijos":
                    $parent = getParam("ID");
                    $this->grdHijos->setWhere("PARENT=$parent");
                    $this->grdHijos->db = $this->db;
                    $this->grdHijos->main();
                    break;
                case "edit":
                    include $this->includePath . "edit.php";
                    break;
                case "new":
                    include $this->includePath . "new.php";
                    break;
                default: //switch file
                    include $this->includePath . "data.php";
                    include $this->includePath . "main.php";
            }
        } else {
            include $this->includePath . "main.php";
        }
    }

}
?>
